#pragma once

#include "KtCommon/LanCode.h"
#include "tinyxml/tinyxml.h"

#ifdef _DEBUG
	#pragma comment(lib,"tinyxmld.lib")
#else
	#pragma comment(lib,"tinyxml.lib")
#endif

const CHAR*		WXmlParser_LoadCommand( TiXmlElement& element,const CHAR* szCommand,int nLength )
{
	if( NULL == szCommand || nLength == 0 )
		return NULL;
	const CHAR *szResult = element.Parse( szCommand,NULL,TIXML_ENCODING_UTF8 );
	if( NULL == szResult )
		return NULL;
	if( strcmp( element.Value(),"cmd") != 0 )
		return NULL;
	return szResult;
}

const CHAR*		WXmlParser_GetCommand( TiXmlElement *pElement,WORD& wCommand )
{
	if( NULL == pElement )
		return NULL;
	int nValue;
	const CHAR* szValue = pElement->Attribute( "id",&nValue );
	if( NULL == szValue )
		return NULL;
	wCommand = nValue;
	return szValue;
}

BOOL			WXmlParser_SetCommand( TiXmlElement *pElement,WORD wCommand )
{
	if( NULL == pElement )
		return FALSE;
	pElement->SetAttribute( "id",wCommand );
	return TRUE;
}

const CHAR*		WXmlParser_GetVersion( TiXmlElement *pElement )
{
	if( NULL == pElement )
		return NULL;
	return pElement->Attribute( "ver" );
}

BOOL			WXmlParser_SetVersion( TiXmlElement *pElement,const CHAR* szVersion )
{
	if( NULL == pElement || NULL == szVersion )
		return FALSE;
	pElement->SetAttribute( "ver",szVersion );
	return TRUE;
}

const CHAR*		WXmlParser_GetSeqnum( TiXmlElement *pElement,int& nSeqnum )
{
	if( NULL == pElement )
		return NULL;
	int nValue;
	const CHAR* szValue = pElement->Attribute( "seq",&nValue );
	if( NULL == szValue )
		return NULL;
	nSeqnum = nValue;
	return szValue;
}

BOOL			WXmlParser_SetSeqnum( TiXmlElement *pElement,int nSeqnum )
{
	if( NULL == pElement )
		return FALSE;
	pElement->SetAttribute( "seq",nSeqnum );
	return TRUE;
}

const CHAR*		WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName )
{
	if( NULL == pElement || NULL == szValueName )
		return NULL;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return NULL;
	return pValueElement->Attribute( "val" );
}

BOOL			WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const CHAR* szValue )
{
	if( NULL == pElement || NULL == szValueName || NULL == szValue )
		return FALSE;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return NULL;
	pValueElement->SetAttribute( "val",szValue );
	return TRUE;
}

BOOL			WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const CHAR* szValue )
{
	if( NULL == pElement || NULL == szValueName || NULL == szValue )
		return FALSE;
	TiXmlElement element("");
	TiXmlNode *pNode = pElement->InsertEndChild( element );
	if( NULL == pNode )
		return FALSE;
	pNode->ToElement()->SetValue( szValueName );
	pNode->ToElement()->SetAttribute( "val",szValue );
	return TRUE;
}

const CHAR*		WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,WCHAR* wszValue,DWORD dwSize )
{
	if( NULL == pElement || NULL == szValueName || NULL == wszValue || dwSize == 0 )
		return NULL;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return NULL;
	const CHAR* szValue = pValueElement->Attribute( "val" );
	if( NULL == szValue )
		return NULL;
	if( CLanCode::ConvertUtf8ToUnicode( szValue,wszValue,dwSize ) <= 0 )
		return NULL;
	return szValue;
}

BOOL			WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const WCHAR* wszValue )
{
	if( NULL == pElement || NULL == szValueName || NULL == wszValue )
		return FALSE;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return FALSE;
	int nSize = wcslen(wszValue)*3+2;
	CHAR *szValue = new CHAR[ nSize ];
	if( NULL == szValue )
		return FALSE;
	if( CLanCode::ConvertUnicodeToUtf8( wszValue,szValue,nSize) <= 0 ){
		delete[] szValue;
		return FALSE;
	}
	pValueElement->SetAttribute( "val",szValue );
	delete[] szValue;
	return TRUE;
}

BOOL			WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const WCHAR* wszValue )
{
	if( NULL == pElement || NULL == szValueName || NULL == wszValue )
		return FALSE;
	TiXmlElement element("");
	TiXmlNode *pNode = pElement->InsertEndChild( element );
	if( NULL == pNode )
		return FALSE;
	pNode->ToElement()->SetValue( szValueName );

	int nSize = wcslen(wszValue)*3+2;
	CHAR *szValue = new CHAR[ nSize ];
	if( NULL == szValue )
		return FALSE;
	if( CLanCode::ConvertUnicodeToUtf8( wszValue,szValue,nSize) <= 0 ){
		delete[] szValue;
		return FALSE;
	}
	pNode->ToElement()->SetAttribute( "val",szValue );
	delete[] szValue;
	return TRUE;
}

const CHAR*		WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,int& nValue )
{
	if( NULL == pElement || NULL == szValueName )
		return NULL;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return NULL;
	return pValueElement->Attribute( "val",&nValue );
}

BOOL			WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,int nValue )
{
	if( NULL == pElement || NULL == szValueName )
		return FALSE;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return FALSE;
	pValueElement->SetAttribute( "val",nValue );
	return TRUE;
}

BOOL			WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,int nValue )
{
	if( NULL == pElement || NULL == szValueName )
		return FALSE;
	TiXmlElement element("");
	TiXmlNode *pNode = pElement->InsertEndChild( element );
	if( NULL == pNode )
		return FALSE;
	pNode->ToElement()->SetValue( szValueName );
	pNode->ToElement()->SetAttribute( "val",nValue );
	return TRUE;
}

const CHAR*		WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,GUID& guid )
{
	if( NULL == pElement || NULL == szValueName )
		return NULL;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return NULL;
	const CHAR* szValue = pValueElement->Attribute( "val" );
	if( NULL == szValue )
		return NULL;
	WCHAR  wszGuid[64];
	if( CLanCode::ConvertUtf8ToUnicode( szValue,wszGuid,64) <= 0 )
		return NULL;
	CLSIDFromString( wszGuid,&guid );
	return szValue;
}

BOOL			WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const GUID& guid )
{
	if( NULL == pElement || NULL == szValueName )
		return FALSE;
	TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
	if( NULL == pValueElement )
		return FALSE;
	WCHAR  *wszGuid = NULL;
	StringFromCLSID( guid,&wszGuid );
	if( NULL == wszGuid )
		return FALSE;
	CHAR szValue[64];
	if( CLanCode::ConvertUnicodeToUtf8( wszGuid,szValue,64 ) <= 0 ){
		CoTaskMemFree( wszGuid );
		return FALSE;
	}
	CoTaskMemFree( wszGuid );
	pValueElement->SetAttribute( "val",szValue );
	return TRUE;
}

BOOL			WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const GUID& guid )
{
	if( NULL == pElement || NULL == szValueName )
		return FALSE;
	TiXmlElement element("");
	TiXmlNode *pNode = pElement->InsertEndChild( element );
	if( NULL == pNode )
		return FALSE;
	pNode->ToElement()->SetValue( szValueName );
	WCHAR  *wszGuid = NULL;
	StringFromCLSID( guid,&wszGuid );
	if( NULL == wszGuid )
		return FALSE;
	CHAR szValue[64];
	if( CLanCode::ConvertUnicodeToUtf8( wszGuid,szValue,64 ) <= 0 ){
		CoTaskMemFree( wszGuid );
		return FALSE;
	}
	CoTaskMemFree( wszGuid );
	pNode->ToElement()->SetAttribute( "val",szValue );
	return TRUE;
}

const CHAR*		WXmlParser_GetValue( TiXmlElement* pElement,const CHAR* szValueName,WCHAR* wszValue,DWORD dwSize )
{
	if( NULL == pElement || NULL == szValueName || NULL == wszValue || dwSize == 0 )
		return NULL;

	const CHAR *szValue = TiXmlGetValue( pElement, szValueName );
	if( NULL == szValue)
		return NULL;

	if( CLanCode::ConvertUtf8ToUnicode( szValue,wszValue,dwSize ) <= 0 )
		return NULL;
	return szValue;
}

const CHAR*		WXmlParser_GetValue( TiXmlElement* pElement,const CHAR* szValueName,DWORD dwSize )
{
	if( NULL == pElement || NULL == szValueName || dwSize == 0 )
		return NULL;
	
	const CHAR *szValue = TiXmlGetValue( pElement, szValueName );
	return szValue;
}

